package com.example.pluginmanager.controller;

import com.common.request.PageParam;
import com.common.result.PageVO;
import com.common.result.ResponseVO;
import com.example.pluginmanager.entity.LoginUser;
import com.example.pluginmanager.entity.Menu;
import com.example.pluginmanager.entity.User;
import com.example.pluginmanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/generateValidateCode")
    public void generateValidateCode(HttpServletResponse response){
        try {
            userService.generateValidateCode(response);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("生成验证码异常");
        }
    }

    @PostMapping("/login")
    public ResponseVO login(@RequestBody LoginUser loginUser){
        String login = userService.login(loginUser);
        return ResponseVO.success(login);
    }


    @GetMapping("/loginOut")
    public ResponseVO loginOut(HttpServletRequest request){
        String access_token = request.getHeader("access_token");
        userService.loginOut(access_token);
        return ResponseVO.success();
    }

    @PostMapping("/query/getUserList")
    public ResponseVO getMenuList(@RequestBody PageParam<User> userPageParam) {
        PageVO<User> userByPage = userService.listUserByPage(userPageParam);
        return ResponseVO.success(userByPage);
    }
}
